import { ApiCall } from "tsrpc";
import { ReqJoinRoom, ResJoinRoom } from "../../../tsrpc/shared/protocols/hall/PtlJoinRoom";
import { hs } from "../../../HallServer";
import { account } from "../../../module/account/Account";

export default async function (call: ApiCall<ReqJoinRoom, ResJoinRoom>) {

    if (!call.req.__ssoToken) {
        call.error("登录令牌不存在", { code: 'SSO_NoNull' });
        return;
    }
    // TODO 
    if(!hs.HallModel.rooms.has(call.req.roomId)) {
        call.error("房间不存在", { code: 'Room_NoExist' });
        return;
    }

    var dUser = account.parseSSO(call.req.__ssoToken);
    if (!dUser) {
        call.error(`登录令牌无效`, { code: 'SSO_Invalid' });
        return;
    }

    const room = hs.HallModel.rooms.get(call.req.roomId)!;
    room.joinRoom(dUser.key);
}